/**
 *Copyright (c) 2024 watereyes
 * workflow is licensed under Mulan PubL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PubL v2.
 * You may obtain a copy of Mulan PubL v2 at:
 *          http://license.coscl.org.cn/MulanPubL-2.0
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
 * EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
 * MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
 * See the Mulan PubL v2 for more details.
 */
package com.koron.workflow.applicationManag.service.impl;

import com.koron.workflow.applicationManag.bean.AdministratorAppBean;
import com.koron.workflow.applicationManag.bean.AdministratorAppQueryBean;
import com.koron.workflow.applicationManag.mapper.AdministratorAppMapper;
import com.koron.workflow.applicationManag.service.AdministratorAppService;
import com.koron.workflow.common.bean.PageBean;
import com.koron.workflow.common.bean.StaffBean;
import com.koron.workflow.common.util.BusinessCode;
import com.koron.workflow.common.util.DisposeBeanUtil;
import org.koron.ebs.mybatis.SessionFactory;
import org.koron.ebs.mybatis.TaskAnnotation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.swan.bean.MessageBean;

import java.util.List;
import java.util.Map;

@Service
public class AdministratorAppServiceImpl implements AdministratorAppService {
    private static final Logger logger = LoggerFactory.getLogger(AdministratorAppServiceImpl.class);

    @TaskAnnotation("queryForList")
    @Override
    public PageBean<AdministratorAppQueryBean> queryForList(SessionFactory factory, String appId, Integer page, Integer pageSize) {
        AdministratorAppMapper mapper = factory.getMapper(AdministratorAppMapper.class);
        List<AdministratorAppQueryBean> list = mapper.queryForList(appId, page, pageSize);
        Integer total = null;
        if (page != null || pageSize != null) {
            total = mapper.countQueryForList(appId);
        }
        return new PageBean<>(list, total);
    }

    @TaskAnnotation("queryForObject")
    @Override
    public AdministratorAppQueryBean queryForObject(SessionFactory factory, String id) {
        return factory.getMapper(AdministratorAppMapper.class).queryForObject(id);
    }

    @TaskAnnotation("save")
    @Override
    public MessageBean save(SessionFactory factory, StaffBean user, AdministratorAppBean bean) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        try {
            if (StringUtils.isEmpty(bean.getAppId()) || StringUtils.isEmpty(bean.getAdministratorId()) ||
                    StringUtils.isEmpty(bean.getDesignRight()) || StringUtils.isEmpty(bean.getWatchRight())) {
                msg.setCode(BusinessCode.EMPTY_PARAM.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM.getDescription());
                return msg;
            }
            DisposeBeanUtil.disposeInsert(user, bean);
            AdministratorAppMapper mapper = factory.getMapper(AdministratorAppMapper.class);
            // 判断当前应用下是否已经存在该管理员
            if (mapper.queryAdminAppByAppidAndAdministratorId(bean.getAppId(), bean.getAdministratorId(), null) > 0) {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("当前应用下已经存在该管理员");
                return msg;
            }
            if (mapper.insert(bean) != 1) {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription(BusinessCode.FAILURE.getDescription());
                return msg;
            }
        } catch (Exception e) {
            factory.close(false);
            logger.error("管理员保存失败", e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }

    @TaskAnnotation("update")
    @Override
    public MessageBean update(SessionFactory factory, StaffBean user, AdministratorAppBean bean) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        try {
            if (StringUtils.isEmpty(bean.getId())) {
                msg.setCode(BusinessCode.EMPTY_PARAM.getCode());
                msg.setDescription("没有获取到当前业务ID");
                return msg;
            }
            if (StringUtils.isEmpty(bean.getAppId()) || StringUtils.isEmpty(bean.getAdministratorId()) ||
                    StringUtils.isEmpty(bean.getDesignRight()) || StringUtils.isEmpty(bean.getWatchRight())) {
                msg.setCode(BusinessCode.EMPTY_PARAM.getCode());
                msg.setDescription(BusinessCode.EMPTY_PARAM.getDescription());
                return msg;
            }
            DisposeBeanUtil.disposeUpdate(user, bean);
            AdministratorAppMapper mapper = factory.getMapper(AdministratorAppMapper.class);
            // 判断当前应用下是否已经存在该管理员
            if (mapper.queryAdminAppByAppidAndAdministratorId(bean.getAppId(), bean.getAdministratorId(), bean.getId()) > 0) {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription("当前应用下已经存在该管理员");
                return msg;
            }
            if (mapper.update(bean) != 1) {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription(BusinessCode.FAILURE.getDescription());
                return msg;
            }
        } catch (Exception e) {
            factory.close(false);
            logger.error("管理员修改失败", e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }

    @TaskAnnotation("delete")
    @Override
    public MessageBean delete(SessionFactory factory, String id) {
        MessageBean msg = MessageBean.create(BusinessCode.SUCCESS.getCode(), BusinessCode.SUCCESS.getDescription(), void.class);
        try {
            if (factory.getMapper(AdministratorAppMapper.class).delete(id) != 1) {
                factory.close(false);
                msg.setCode(BusinessCode.FAILURE.getCode());
                msg.setDescription(BusinessCode.FAILURE.getDescription());
                return msg;
            }
        } catch (Exception e) {
            factory.close(false);
            logger.error("管理员删除失败", e);
            msg.setCode(BusinessCode.EXCEPTION.getCode());
            msg.setDescription(BusinessCode.EXCEPTION.getDescription());
        }
        return msg;
    }

    @TaskAnnotation("queryAdministrators")
    @Override
    public List<Map> queryAdministrators(SessionFactory factory) {
        return factory.getMapper(AdministratorAppMapper.class).queryAdministrators();
    }
}
